#pragma once
#include <vector>

typedef unsigned int  UINT;
typedef unsigned short USHORT;
typedef unsigned char UCHAR;

using namespace std;

typedef struct
{
    UINT        tag;
    USHORT      endianflag;   //big: FEFF  little:FFFE
    USHORT      padding;
    UINT        filesize;
    USHORT      hlen;
    USHORT      sectioncount;
} BrresHeader ;


typedef  struct
{
    UINT            tag;
    UINT            ssize;
} BrresSectionHeader;


typedef  struct
{
    USHORT          entryid;
    USHORT          unknown;
    USHORT          lindex;
    USHORT          rindex;
    UINT            nameoffset;
    UINT            dataoffset;     //all offset is to brres index group head
} BrresIndex;

typedef  struct
{
    UINT            igsize;
    UINT            icount;
    BrresIndex      refindex;
} BrresIndexGroupHead;


typedef  struct
{
    char            tag[4];
    UINT            sfsize;
    UINT            sfver;
    UINT            bpoffset;     //the offset to brres file head
}   BrresSubFileHeader;

typedef  struct  _tagBrresNode
{
    char                    *nodename;
    UINT                    offset;
    UINT                    childcount;
    _tagBrresNode           *parent;
    vector<_tagBrresNode*>  children;
}  BrresNode;


typedef struct
{
    //BrresHeader     header;
    BrresNode       *root;
    UCHAR           *pdata;
}  BrresFile;


extern "C"  bool readbrres(char *bres , UINT bsize , BrresFile  **r);
extern "C"  bool readbrresfromfile(const char *filename , BrresFile **r);  //read brres from a file
extern "C"  void freebrres(BrresFile  *r);

extern "C"  BrresNode*      GetDirNode(char *bres,BrresNode *parent,UINT offset);
extern "C"  BrresNode*      GetSubFileNode(char *bres,BrresNode *parent,UINT offset);
extern "C"  void            FreeBrresNode(BrresNode *bnode );
